/*
 * CreateSimpleProduct.java
 *
 * Created on 13 novembre 2007, 17:39
 */
package com.reimscopie.console ;

import javax.naming.* ;
import javax.transaction.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.interfaces.* ;


/**
 * This command create a new simple product.
 */
public class CreateSimpleProduct extends BeanCommand {
  
  /**
   * Create a new <code>CreateProduct</code> instance.
   */
  public CreateSimpleProduct() {
    
    super("com.reimscopie.beans.session.ReimsCopieAdminBean",
        ReimsCopieAdminRemote.class) ;
    
  }
  
  
  /**
   * Return the name of this command. The name is the first word in the shell
   * command line.
   *
   * @return The command name.
   */
  public String getName() {
    return "create-simple-product" ;
  }
  
  
  /**
   * Return a short help message about this command.
   *
   * @return The command help.
   */
  public String getHelp() {
    return "Create a new simple product" ;
  }
  
  
  /**
   * Return the command usage. This string must give all command options with
   * a description for each option.
   *
   * @return The command usage.
   */
  public String getUsage() {
    
    StringBuilder builder = new StringBuilder() ;
        
    builder.append("create-simple-product name descr price stock\n") ;
    builder.append("  name     The product name\n") ;
    builder.append("  descr    The product long description\n") ;
    builder.append("  pict     The product's picture\n") ;
    builder.append("  price    The sale price (float)\n") ;
    builder.append("  stock    The initial stock (int)\n") ;
    
    return builder.toString() ;
  }
  
  
  /**
   * Execute this command. A new simple product is created if the arguments
   * are valid.
   *
   * @param args The arguments given in the command line.
   * @param output The command output.
   *
   * @exception CommandException Arguments are invalid.
   */
  public void execute(String[] args,ConsoleOutput output)
  throws CommandException {
    
    String name  = requireParameter("name",0,args) ;
    String desc  = requireParameter("desc",1,args) ;
    String pict  = requireParameter("pict",2,args) ;
    float  price = requireFloatParameter("price",3,args) ;
    int   stock = requireIntegerParameter("stock",4,args) ;
    
    
    // product instance
    SimpleProduct product = new SimpleProduct(
          name,
          desc,
          pict,
          true,
          price,
          stock
    ) ;
    
    doCreate(product,output) ;
    
  }
  
  
  /**
   * Create the product.
   *
   * @param product The product to create.
   * @param output The command output.
   */
  private void doCreate(SimpleProduct product,ConsoleOutput output) {
    
    try {
      // get JNDI initial context
      Context context = new InitialContext() ;
      UserTransaction transaction = getTransaction(context) ;
      ReimsCopieAdminRemote bean = (ReimsCopieAdminRemote)getBean(context) ;
      
      if (bean == null) {
        output.printError("product not created (no bean)",true) ;
        return ;
      }
      
      try {
        transaction.begin() ;
        bean.createSimpleProduct(product) ;
        transaction.commit() ;
        output.print("Done",true) ;
      }
      catch (Exception e) {
        output.printError("Creation failed",true) ;
        try {
          transaction.rollback() ;
        }
        catch (SystemException ex) {
          output.printError(ex) ;
        }
        output.printError(e) ;
      }
      
    }
    catch (NamingException e) {
      output.printError("Cannot get initial context for JNDI") ;
      output.printError(e) ;
      return ;
    }
  }
  
}
